//////////////////////////////////////////////////////////////////////
//!\file AlbumDataModel.cpp
//!\brief Holds flac track and albumdata.
//!\brief vector aka album.
//!\brief Use the getter to obtain a vector of flac albums.
//!\author timmsusa@googlemail.com
//////////////////////////////////////////////////////////////////////

#include "AlbumDataModel.hpp"
#include <QDebug>

AlbumDataModel::AlbumDataModel(const QString& path):
    m_Persistence(),
    m_DeepScanEnabled(m_Persistence.getEnableDeepScan()),
    m_Path(path),
    m_FlacAlbumList()
{}



const QVector < Album >& AlbumDataModel::getAlbumList()const
{
    return m_FlacAlbumList;
}

void AlbumDataModel::clearFlacAlbumList()
{
    m_FlacAlbumList.clear();
}

void AlbumDataModel::addAlbum(const Album &album)
{
    if (!album.getTrackList().empty() && !m_DeepScanEnabled)
    {
        m_FlacAlbumList.push_back(album);
    }
    else if (!album.getFlacTrackList().empty() && m_DeepScanEnabled)
    {
        m_FlacAlbumList.push_back(album);
        //qDebug() << "FLAC Album found: " <<  album.getName() << " with size: " << album.getFlacTrackList().size();
    }
    else
    {
    }
}

void AlbumDataModel::addFlacTrackToAlbum(const QFileInfo fi, Album &album)
{
    QString filePath = fi.absoluteFilePath();

    if (FlacMetadataManager::isFlacValid(filePath))
    {
        QString metaMd5 = FlacMetadataManager::getTrackMd5ByPath(filePath);
        QString md5 = FlacMetadataManager::extractMd5(filePath);
        // qDebug() << "AlbumDataModel::addFlacTrackToAlbum " << md5  <<  " MusicFiles: " <<  fi.completeBaseName() << "MD5: " << metaMd5;
        FlacTrack  flacTrack( "" , md5, fi.completeBaseName() + ".flac",  metaMd5);
        album.addFlacTrack(flacTrack.get());
        //qDebug() << "DeepScan! " << flacTrack.m_File << " found!";
    }
}

const AlbumDataModel& AlbumDataModel::get()const
{
    return *this;
}

void AlbumDataModel::slotDeepScanEnabled(bool enabled)
{
    qDebug() << "AlbumDataModel::slotDeepScanEnabled: " << enabled;
    m_DeepScanEnabled = enabled;
}

